package Formularios;

import BD.clientes;
import BD.Validacao;
import BD.pedidos;
import BD.utilsbd;

import java.util.ArrayList;
import javax.swing.JOptionPane;

/**
 *
 * @author Gusttavo
 */
public class FormClientes extends javax.swing.JFrame {
    private boolean Incluindo;
    private int ponteiro = 0;
    private ArrayList<clientes> listClientes = new ArrayList<clientes>();
    private Validacao vldc = new Validacao();
    

    /**
     * Creates new form FormClientes
     */
    public FormClientes() {
        initComponents();
        TravaBtns(false, true, false);
        setLocationRelativeTo(null);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        labelID = new javax.swing.JLabel();
        jtcodigo = new javax.swing.JTextField();
        labelNome = new javax.swing.JLabel();
        jtNome = new javax.swing.JTextField();
        labelEndereco = new javax.swing.JLabel();
        jtEndereco = new javax.swing.JTextField();
        labelTelRes = new javax.swing.JLabel();
        labelTelCel = new javax.swing.JLabel();
        Separador2 = new javax.swing.JSeparator();
        jpnlPesq = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jtPesq = new javax.swing.JTextField();
        jbtnPesquisa = new javax.swing.JButton();
        jpNavegacao = new javax.swing.JPanel();
        jbtnFirst = new javax.swing.JButton();
        jbtnPrior = new javax.swing.JButton();
        jbtnNext = new javax.swing.JButton();
        jbtnLast = new javax.swing.JButton();
        Separador1 = new javax.swing.JSeparator();
        jbtncancelar = new javax.swing.JButton();
        jbtnexcluir = new javax.swing.JButton();
        jbtnGravar = new javax.swing.JButton();
        jbtnAlterar = new javax.swing.JButton();
        jbtnNovo = new javax.swing.JButton();
        jtTelRes = new javax.swing.JFormattedTextField();
        jtTelCel = new javax.swing.JFormattedTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Master Pizza - Cadastro de Clientes");

        labelID.setText("Codigo");

        labelNome.setText("Nome");

        jtNome.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtNomeActionPerformed(evt);
            }
        });

        labelEndereco.setText("Endereço");

        labelTelRes.setText("Telefone Residencial");

        labelTelCel.setText("Telefone Celular");

        jLabel1.setText("Descrição");

        jtPesq.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtPesqActionPerformed(evt);
            }
        });

        jbtnPesquisa.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icones/Pesquisa.png"))); // NOI18N
        jbtnPesquisa.setText("Pesquisar");
        jbtnPesquisa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnPesquisaActionPerformed(evt);
            }
        });

        jbtnFirst.setText("<<");
        jbtnFirst.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnFirstActionPerformed(evt);
            }
        });

        jbtnPrior.setText("<");
        jbtnPrior.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnPriorActionPerformed(evt);
            }
        });

        jbtnNext.setText(">");
        jbtnNext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnNextActionPerformed(evt);
            }
        });

        jbtnLast.setText(">>");
        jbtnLast.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnLastActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jpNavegacaoLayout = new javax.swing.GroupLayout(jpNavegacao);
        jpNavegacao.setLayout(jpNavegacaoLayout);
        jpNavegacaoLayout.setHorizontalGroup(
            jpNavegacaoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpNavegacaoLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jbtnFirst)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtnPrior)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtnNext)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtnLast)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jpNavegacaoLayout.setVerticalGroup(
            jpNavegacaoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jpNavegacaoLayout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addGroup(jpNavegacaoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtnFirst)
                    .addComponent(jbtnPrior)
                    .addComponent(jbtnNext)
                    .addComponent(jbtnLast)))
        );

        javax.swing.GroupLayout jpnlPesqLayout = new javax.swing.GroupLayout(jpnlPesq);
        jpnlPesq.setLayout(jpnlPesqLayout);
        jpnlPesqLayout.setHorizontalGroup(
            jpnlPesqLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jpnlPesqLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jpnlPesqLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jpnlPesqLayout.createSequentialGroup()
                        .addComponent(jtPesq)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jbtnPesquisa))
                    .addGroup(jpnlPesqLayout.createSequentialGroup()
                        .addGap(54, 54, 54)
                        .addComponent(jpNavegacao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
            .addComponent(Separador1)
        );
        jpnlPesqLayout.setVerticalGroup(
            jpnlPesqLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpnlPesqLayout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(jpnlPesqLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jtPesq, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbtnPesquisa))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jpNavegacao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 12, Short.MAX_VALUE)
                .addComponent(Separador1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jbtncancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icones/Cancelar.png"))); // NOI18N
        jbtncancelar.setText("Cancelar");
        jbtncancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtncancelarActionPerformed(evt);
            }
        });

        jbtnexcluir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icones/Excluir.png"))); // NOI18N
        jbtnexcluir.setText("Excluir");
        jbtnexcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnexcluirActionPerformed(evt);
            }
        });

        jbtnGravar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icones/Gravar.png"))); // NOI18N
        jbtnGravar.setText("Gravar");
        jbtnGravar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnGravarActionPerformed(evt);
            }
        });

        jbtnAlterar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icones/Alterar.png"))); // NOI18N
        jbtnAlterar.setText("Alterar");
        jbtnAlterar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnAlterarActionPerformed(evt);
            }
        });

        jbtnNovo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icones/Incluir.png"))); // NOI18N
        jbtnNovo.setText("Novo");
        jbtnNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnNovoActionPerformed(evt);
            }
        });

        try {
            jtTelRes.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("(##)####-####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        try {
            jtTelCel.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("(##)####-####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(labelID)
                            .addComponent(jtcodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(labelNome)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addComponent(jtNome))
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jbtnNovo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbtnAlterar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jbtnGravar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jbtnexcluir)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jbtncancelar)
                        .addGap(26, 26, 26))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jtEndereco)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(labelTelRes)
                                    .addComponent(jtTelRes, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(labelTelCel)
                                        .addGap(122, 122, 122))
                                    .addComponent(jtTelCel)))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(labelEndereco)
                                .addGap(0, 0, Short.MAX_VALUE)))
                        .addContainerGap())))
            .addComponent(jpnlPesq, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(Separador2, javax.swing.GroupLayout.Alignment.TRAILING)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jpnlPesq, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(labelID)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jtcodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(labelNome)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jtNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labelEndereco)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtEndereco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelTelRes)
                    .addComponent(labelTelCel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtTelRes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtTelCel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(Separador2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtncancelar)
                    .addComponent(jbtnexcluir)
                    .addComponent(jbtnGravar)
                    .addComponent(jbtnAlterar)
                    .addComponent(jbtnNovo))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbtnPesquisaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnPesquisaActionPerformed
        clientes cli = new clientes();
        cli.setNome(jtPesq.getText());
        listClientes = cli.getItens();
        TravaBtns(false, true, true);
        jbtnFirstActionPerformed(evt);
    }//GEN-LAST:event_jbtnPesquisaActionPerformed

    private void TravaBtns(boolean incluindo, boolean navegando, boolean pesquisando) {
        jtcodigo.setEditable(false);
        jtNome.setEditable(incluindo);
        jtTelCel.setEditable(incluindo);
        jtTelRes.setEditable(incluindo);
        jtEndereco.setEditable(incluindo);

//        jbtnPrior.setVisible(!incluindo);
//        jbtnNext.setVisible(!incluindo);
//        jbtnLast.setVisible(!incluindo);
//        jbtnFirst.setVisible(!incluindo);
        jpNavegacao.setVisible(!incluindo);

        if (incluindo) {
            jbtncancelar.setVisible(true);
            jbtnGravar.setVisible(true);
            jpnlPesq.setVisible(false);
            jbtnexcluir.setVisible(false);
            jbtnNovo.setVisible(false);
            jbtnAlterar.setVisible(false);
        } else if (navegando) {
            jbtnAlterar.setVisible(true);
            jbtncancelar.setVisible(false);
            jbtnGravar.setVisible(false);
            jpnlPesq.setVisible(true);
            jbtnexcluir.setVisible(true);
            jbtnNovo.setVisible(true);
        }
        
        //jbtnPesquiza.enable(pTipo);
    }

    private void movPonteiro(boolean first, boolean prior, boolean next, boolean last) {
        if (listClientes.size() > 0) {
            if (first) {
                this.ponteiro = 0;
            } else if (prior) {
                this.ponteiro = this.ponteiro - 1;
                if (this.ponteiro < 0) {
                    this.ponteiro = 0;
                }
            } else if (next) {
                this.ponteiro = this.ponteiro + 1;
                if (this.ponteiro > (listClientes.size() - 1)) {
                    this.ponteiro = (listClientes.size() - 1);
                }
            } else if (last) {
                this.ponteiro = (listClientes.size() - 1);
            }

            this.jtcodigo.setText(Integer.toString(listClientes.get(this.ponteiro).getId_cliente()));
            this.jtNome.setText(listClientes.get(this.ponteiro).getNome());
            this.jtEndereco.setText(listClientes.get(this.ponteiro).getEndereco());
            this.jtTelCel.setText(listClientes.get(this.ponteiro).getCelular());
            this.jtTelRes.setText(listClientes.get(this.ponteiro).getFone());            
        } else {
            limpaCampo();
        }
    }
    private boolean Validacao(){
       return (vldc.Valida(jtNome.getText(), "Nome") && vldc.Valida(jtEndereco.getText(), "Endereço"));
    }    
    private void limpaCampo() {
        this.jtcodigo.setText("");
        this.jtNome.setText("");
        this.jtEndereco.setText("");
        this.jtTelCel.setText("");
        this.jtTelRes.setText("");        
        this.jtPesq.setText("");
    }
    
    
    private void jbtnFirstActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnFirstActionPerformed
        movPonteiro(true, false, false, false);
    }//GEN-LAST:event_jbtnFirstActionPerformed

    private void jbtnPriorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnPriorActionPerformed
        movPonteiro(false, true, false, false);
    }//GEN-LAST:event_jbtnPriorActionPerformed

    private void jbtnNextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnNextActionPerformed
        movPonteiro(false, false, true, false);
    }//GEN-LAST:event_jbtnNextActionPerformed

    private void jbtnLastActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnLastActionPerformed
        movPonteiro(false, false, false, true);
    }//GEN-LAST:event_jbtnLastActionPerformed

    private void jbtncancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtncancelarActionPerformed
        Incluindo = false;
        TravaBtns(false, true, false);
        limpaCampo();
    }//GEN-LAST:event_jbtncancelarActionPerformed

    private void jbtnexcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnexcluirActionPerformed

        if (listClientes.size() <= 0) {
            JOptionPane.showMessageDialog(null, "Nenhum registro Encontrado");
        }
        else{
            
        pedidos pd = new pedidos();    
        pd.setSiuacao("");// todo o historico
        pd.setId_clientes(listClientes.get(ponteiro).getId_cliente());
//        ArrayList<pedidos> listPd = new ArrayList<pedidos>();
  //      listPd = pd.getItens();
//        if(listPd.get(0).getId_pedidos() > 0){
//        System.out.println(pd.getItens().size());
        if(pd.getItens().size() > 0){
            JOptionPane.showMessageDialog(null,"<<IMPOSSÍVEL>> Existe pedidos com esse cliente ");
            return;
        };
        
            if(JOptionPane.showConfirmDialog(null,"Deseja exluir o Cliente: "+jtNome.getText()) == 0)
            {
                if (listClientes.get(ponteiro).excluir()){
                    JOptionPane.showMessageDialog(null,"Operação bem sucedida");
                    limpaCampo();
                    jbtnPesquisaActionPerformed(evt);
                }
                else
                JOptionPane.showMessageDialog(null,"Erro ao excluir registro");
            }
        }
    }//GEN-LAST:event_jbtnexcluirActionPerformed
    private boolean Val(){
        utilsbd utlb = new utilsbd();
        if(Incluindo){
            if(!utlb.checaExiste("clientes","  nome like'%"+this.jtNome.getText()+ "%' and fone like'%"+this.jtTelRes.getText()+"%' and celular like'%"+this.jtTelCel.getText()+"%'")){
                utlb.msg("um Cliente");
                return false;
            };
            if(!utlb.checaExiste("clientes","  fone like'%"+this.jtTelRes.getText()+"%'")){
                utlb.mensage("Ja exixte um Cliente com o (Telefone) informado.Verifique!");
                return false;
            };

            if(!utlb.checaExiste("clientes","  celular like'%"+this.jtTelCel.getText()+"%'")){
                utlb.mensage("Ja exixte um Cliente com o (Celular) informado.Verifique!");
                return false;
            };
        }
        else{                                
            if(!utlb.checaExiste("clientes","id_clientes <>'"+this.jtcodigo.getText() +"' and nome like'%"+this.jtNome.getText()+ "%' and fone like'%"+this.jtTelRes.getText()+"%' and celular like'%"+this.jtTelCel.getText()+"%'")){
                utlb.msg("um Cliente");
                return false;
            };
            if(!utlb.checaExiste("clientes","id_clientes <>'"+this.jtcodigo.getText() +"' and fone like'%"+this.jtTelRes.getText()+"%'")){
                utlb.mensage("Ja exixte um Cliente com o (Telefone) informado.Verifique!");
                return false;
            };

            if(!utlb.checaExiste("clientes","id_clientes <>'"+this.jtcodigo.getText() +"' and celular like'%"+this.jtTelCel.getText()+"%'")){
                utlb.mensage("Ja exixte um Cliente com o (Celular) informado.Verifique!");
                return false;
            };            
        }
        return  true;        
    }
    private void jbtnGravarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnGravarActionPerformed
        if (Validacao()) {
            clientes cli = new clientes();

            cli.setNome(this.jtNome.getText());
            cli.setCelular(this.jtTelCel.getText());
            cli.setEndereco(this.jtEndereco.getText());
            cli.setFone(this.jtTelRes.getText());
            
            if(! Val())
               return;

            if (Incluindo) {
                if (cli.incluir()) {
                    TravaBtns(false, true, false);

                    jtPesq.setText(cli.getNome());
                    jbtnPesquisaActionPerformed(evt);
                    jtPesq.setText("");

                    JOptionPane.showMessageDialog(null, "Cliente Cadastrado com Sucesso !");

                } else {
                    JOptionPane.showMessageDialog(null, "Erro ao Cadastrar Cliente");
                }
            } else {
            cli.setId_cliente(Integer.parseInt(this.jtcodigo.getText()));          
                if (cli.alterar()) {

                    jtPesq.setText(cli.getNome());
                    jbtnPesquisaActionPerformed(evt);
                    jtPesq.setText("");

                    JOptionPane.showMessageDialog(null, "Cliente Alterado com Sucesso !");
                } else {
                    JOptionPane.showMessageDialog(null, "Erro ao Alterar o registro");
                }
            }

        }
    }//GEN-LAST:event_jbtnGravarActionPerformed

    private void jbtnAlterarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnAlterarActionPerformed
        if (listClientes.size() > 0) {
            TravaBtns(true, false, false);
            Incluindo = false;
        } else {
            JOptionPane.showMessageDialog(null, "Nenhum registro Encontrado");
        }
    }//GEN-LAST:event_jbtnAlterarActionPerformed

    private void jbtnNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnNovoActionPerformed
        Incluindo = true;
        TravaBtns(true, false, false);
        limpaCampo();
    }//GEN-LAST:event_jbtnNovoActionPerformed

    private void jtPesqActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtPesqActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jtPesqActionPerformed

    private void jtNomeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtNomeActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jtNomeActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FormClientes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FormClientes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FormClientes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FormClientes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FormClientes().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JSeparator Separador1;
    private javax.swing.JSeparator Separador2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JButton jbtnAlterar;
    private javax.swing.JButton jbtnFirst;
    private javax.swing.JButton jbtnGravar;
    private javax.swing.JButton jbtnLast;
    private javax.swing.JButton jbtnNext;
    private javax.swing.JButton jbtnNovo;
    private javax.swing.JButton jbtnPesquisa;
    private javax.swing.JButton jbtnPrior;
    private javax.swing.JButton jbtncancelar;
    private javax.swing.JButton jbtnexcluir;
    private javax.swing.JPanel jpNavegacao;
    private javax.swing.JPanel jpnlPesq;
    private javax.swing.JTextField jtEndereco;
    private javax.swing.JTextField jtNome;
    private javax.swing.JTextField jtPesq;
    private javax.swing.JFormattedTextField jtTelCel;
    private javax.swing.JFormattedTextField jtTelRes;
    private javax.swing.JTextField jtcodigo;
    private javax.swing.JLabel labelEndereco;
    private javax.swing.JLabel labelID;
    private javax.swing.JLabel labelNome;
    private javax.swing.JLabel labelTelCel;
    private javax.swing.JLabel labelTelRes;
    // End of variables declaration//GEN-END:variables
}
